﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Windows.Forms;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.TeamFoundation.VersionControl.Client;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;

namespace TFSPlus.Commands
{
    public class GetLatestForced : IAddinUICommand
    {
        #region IAddinUICommand Members

        public void UISetup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance, EnvDTE80.Commands2 commands, object[] contextGUIDS)
        {
            // Get Latest Forced
            Command getLatestForcedCommand = commands.AddNamedCommand2(addInInstance, "GetLatestForced", "Get Latest Version (Forced)", "Retrieves the latest version for the item, even if local version matches the specified version.", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
            if (getLatestForcedCommand != null)
            {
                // Add command to Item shortcut menu
                Connect.AddCommandToCommandBarMimic(getLatestForcedCommand, "Item", "Get Latest Version");

                // Add command to Code Window's Source Control sub menu
                CommandBarControl control = Connect.GetCommandBarControl("Item", "Get Latest Version", null);
                int faceId = (control != null && control is CommandBarButton) ? ((CommandBarButton)control).FaceId : 59;

                CommandBar commandBar = Connect.GetCommandBar("Code Window", new string[] { "Source Control" });
                int index = 1;
                CommandBarButton shortcutCtrl = (CommandBarButton)getLatestForcedCommand.AddControl(commandBar, index);
                shortcutCtrl.FaceId = faceId;

                // Add command to Source Control Explorer context menu
                Connect.AddCommandToCommandBarMimic(getLatestForcedCommand, "Source Control Explorer", "Get Latest Version");
            }
        }


        public void Startup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance)
        {
            
        }

        public EnvDTE.vsCommandStatus QueryStatus()
        {
            if (Connect.ApplicationObject == null)
                return vsCommandStatus.vsCommandStatusInvisible;

            Window window = Connect.ApplicationObject.ActiveWindow;
            if (window == null)
                return vsCommandStatus.vsCommandStatusInvisible;

            int count = 0;
            if (window.ObjectKind == EnvDTE.Constants.vsWindowKindSolutionExplorer)
            {
                UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
                count = ((object[])hierarchy.SelectedItems).Length;
            }
            else if (window.ObjectKind == EnvDTE.Constants.vsDocumentKindText)
            {
                count = 1;
            }
            else if (window.Caption.StartsWith("Source Control Explorer"))
            {
                count = Connect.VersionControlExt.Explorer.SelectedItems.Length;
            }

            if (count == 1)
                return vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
            else if (count == 0)
                return vsCommandStatus.vsCommandStatusInvisible | vsCommandStatus.vsCommandStatusSupported;
            else
                return vsCommandStatus.vsCommandStatusSupported;
             
        }

        public void Exec()
        {
            if (Connect.VersionControlExt.SolutionWorkspace == null)
                return;

            Window window = Connect.ApplicationObject.ActiveWindow;
            if (window == null)
                return;
            if (window.ObjectKind == EnvDTE.Constants.vsWindowKindSolutionExplorer)
            {
                UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
                foreach (UIHierarchyItem selectedItem in (object[])hierarchy.SelectedItems)
                {
                    string filePath = TFSPlusCommon.GetFilePath(selectedItem.Object);
                    string serverPath = Connect.VersionControlExt.SolutionWorkspace.GetServerItemForLocalItem(filePath);
                    if (!String.IsNullOrEmpty(serverPath))
                    {
//                        if (MessageBox.Show("Get Latest for item " + serverPath + "?", "Get Latest Forced", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Connect.VersionControlExt.SolutionWorkspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite);
                        }
                    }
                }
            }
            else if (window.ObjectKind == EnvDTE.Constants.vsDocumentKindText)
            {
                Document doc = Connect.ApplicationObject.ActiveDocument;
                if (doc != null)
                {
                    string serverPath = Connect.VersionControlExt.SolutionWorkspace.GetServerItemForLocalItem(doc.FullName);
                    if (!String.IsNullOrEmpty(serverPath))
                    {
                        Connect.VersionControlExt.SolutionWorkspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite);
                    }
                }
            }
            else if (window.Caption.StartsWith("Source Control Explorer"))
            {
                foreach (VersionControlExplorerItem vcei in Connect.VersionControlExt.Explorer.SelectedItems)
                {
                    if (!String.IsNullOrEmpty(vcei.SourceServerPath))
                    {
                        Connect.VersionControlExt.SolutionWorkspace.Get(new string[] { vcei.SourceServerPath }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite);
                    }
                }
            }
        }

        public void Disconnect(DTE2 applicationObject, AddIn addInInstance)
        {
        }

        #endregion
    }
}
